package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.dc.bobaoge.base.entity.core.po.ContactUser;
import com.dc.bobaoge.base.entity.message.common.po.MessageComment;
import com.dc.bobaoge.base.mapper.message.MessageCommentMapper;
import com.dc.bobaoge.project.base.core.mq.RabbitMqUtil;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessageComment;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageCommentMapper;
import com.dc.bobaoge.project.base.core.util.MessageCommonUtil;
import com.dc.bobaoge.project.base.core.util.model.MessageSimpleBaseInfoModel;
import com.dc.bobaoge.project.v2.to.v3.business.core.method.user.UserResolveMethod;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.system.user.entity.po.SystemUserInfo;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.entity.tuple.Tuple2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * MessageCommentResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 11:33:52
 */
@Slf4j
@Component
public class MessageCommentResolveMethod implements ResolveMethod {

    /**
     * 前缀数组
     */
    private static final List<String> PREFIX_CONTENT_ARRAY = new ArrayList<>() {{
        add("[楼主评论] ");
        add("[不是楼主评论] ");
    }};

    @Resource
    private OldCoreMessageCommentMapper oldCoreMessageCommentMapper;

    @Resource
    private UserResolveMethod userResolveMethod;

    @Resource
    private MessageCommentMapper messageCommentMapper;

    @Resource
    private MessageCommonUtil messageCommonUtil;
    @Resource
    private RabbitMqUtil rabbitMqUtil;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询原信息
        var oldCoreMessageComment = oldCoreMessageCommentMapper.selectById(trigger.getTargetId());
        if (oldCoreMessageComment == null) {
            throw new BusinessSimpleException("留言不存在: " + trigger.getTargetId());
        }
        // 查询当前评论是否已经存在了
        var comment = messageCommentMapper.selectById(oldCoreMessageComment.getCommentId());
        if (comment == null) {
            // 新增
            add(trigger, oldCoreMessageComment);
            return;
        }
        // 获取消息的基础信息
        var baseInfo = messageCommonUtil.getSimple(oldCoreMessageComment.getCommentMsgId());
        // 查询评论的用户信息
        var user = userResolveMethod.getByUserId(oldCoreMessageComment.getCommentUserId());
        buildCommon(comment, baseInfo, user, oldCoreMessageComment);
        // 修改信息
        messageCommentMapper.updateById(comment);
    }

    /**
     * 新增留言信息
     *
     * @param trigger               日志信息
     * @param oldCoreMessageComment 旧的评论信息
     */
    private void add(SyncTrigger trigger, OldCoreMessageComment oldCoreMessageComment) {
        // 获取消息的基础信息
        var baseInfo = messageCommonUtil.getSimple(oldCoreMessageComment.getCommentMsgId());
        // 查询评论的用户信息
        var user = userResolveMethod.getByUserId(oldCoreMessageComment.getCommentUserId());
        // 查询留言信息
        var comment = buildCommon(new MessageComment(), baseInfo, user, oldCoreMessageComment);
        // 新增留言信息
        messageCommentMapper.insert(comment);
        // 同步留言新增的数量信息
        rabbitMqUtil.addCommentCount(baseInfo.getId(), baseInfo.getQuarter(), baseInfo.getType());
    }

    /**
     * 处理公共信息
     *
     * @param comment               新的留言信息
     * @param message               消息信息
     * @param user                  用户信息
     * @param oldCoreMessageComment 旧的留言信息
     * @return 新的留言信息
     */
    private MessageComment buildCommon(
            MessageComment comment,
            MessageSimpleBaseInfoModel message,
            Tuple2<SystemUserInfo, ContactUser> user,
            OldCoreMessageComment oldCoreMessageComment
    ) {
        comment.setId(oldCoreMessageComment.getCommentId());
        comment.setFkMessageId(oldCoreMessageComment.getCommentMsgId());
        comment.setMessageType(message.getType());
        comment.setMessageQuarter(message.getQuarter());
        // 处理评论的内容
        var resultContent = PREFIX_CONTENT_ARRAY.stream().reduce(oldCoreMessageComment.getCommentContent(), (result, prefix) -> {
            if (result.startsWith(prefix)) {
                return result.substring(prefix.length());
            } else {
                return result;
            }
        });
        comment.setContent(resultContent);
        comment.setFkUserId(oldCoreMessageComment.getCommentUserId());
        comment.setUserName(user.value1().getName());
        comment.setPhone(oldCoreMessageComment.getCommentUserPhone());
        comment.setAuditStatus(oldCoreMessageComment.getCommentIsDelete() + 1);
        comment.setAuditTime(oldCoreMessageComment.getCommentDate());
        return comment;
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_comment";
    }

}
